package weaver.interfaces.zhlx.duss.action.salesmodule.crm;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.CrmUtil;
import weaver.interfaces.zhlx.duss.utils.PropUltraUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author dusongsong
 * @date 2024年12月23日 09:02
 */
public class Wea02CusSpecApproReaPhaseAction extends BaseBean implements Action {
    private static final String RES_CODE_ZERO = "0";

    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>Wea02CusSpecApproReaPhaseAction>>>>>requestid=" + requestid);
        try {
            RecordSet rs = new RecordSet();
            String message = "调用CRM系统接口返回:";
            CrmUtil crmUtil = new CrmUtil();
            rs.executeQuery("select sqdbh,sqrgh,date1,gdrq,wwkhbm,tempmm,qualengineeringmm," +
                    "cjmm1wb,qmpmm1wb from " + tablename + " where requestid=?", requestid);
            if (rs.next()) {
                String sqdbh = Util.null2String(rs.getString("sqdbh"));//申请单编号
                String sqrgh = Util.null2String(rs.getString("sqrgh"));//申请人工号
                String date1 = Util.null2String(rs.getString("date1"));//申请日期
                String gdrq = Util.null2String(rs.getString("gdrq"));//归档日期
                String wwkhbm = Util.null2String(rs.getString("wwkhbm"));//五位客户编码
                String tempmm = Util.null2String(rs.getString("tempmm"));//Temp.MM
                String qualengineeringmm = Util.null2String(rs.getString("qualengineeringmm"));//Qual./Engineering.MM
                String cjmm1wb = Util.null2String(rs.getString("cjmm1wb"));//长晶MM
                String qmpmm1wb = Util.null2String(rs.getString("qmpmm1wb"));//切磨抛MM

                JSONObject dataJson = new JSONObject();
                JSONObject object_data = new JSONObject();
                object_data.put("dataObjectApiName", "spec_review_size12_02__c");
                object_data.put("review_no__c", sqdbh);
                object_data.put("applied_employee_id__c", sqrgh);
                date1 = !"".equals(date1) ? date1 + " 00:00:00" : date1;
                object_data.put("application_date__c", crmUtil.dateToStamp(date1));
                object_data.put("process_start_time__c", crmUtil.dateToStamp(date1));
                gdrq = !"".equals(gdrq) ? gdrq + " 00:00:00" : gdrq;
                object_data.put("process_end_time__c",crmUtil.dateToStamp(gdrq));
                object_data.put("account_no__c", wwkhbm);
                object_data.put("temp_part_number__c", tempmm);
                object_data.put("engineeing_mm__c", qualengineeringmm);
                object_data.put("grow_crystal_mm__c", cjmm1wb);
                object_data.put("polishing_mm__c", qmpmm1wb);
                JSONObject propVal = PropUltraUtil.getPropMainVal("zhlxcrm");
                String username = propVal.getString("username");
                object_data.put("is_deleted", false);
                object_data.put("owner", username.split(","));
                object_data.put("ec_approvalStatus__c", "3");
                object_data.put("record_type", "default__c");
				dataJson.put("skipDataStatusValidate", true);
                dataJson.put("object_data", object_data);
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "Wea02CusSpecApproReaPhaseAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>Wea02CusSpecApproReaPhaseAction>>>>>dataJson=" + dataJson);
                JSONObject resultJson = crmUtil.excuteHandler(dataJson, dataMap, "Wea02CusSpecApproReaPhaseAction");
                writeLog(">>>>>Wea02CusSpecApproReaPhaseAction>>>>>resultJson=" + resultJson);
                String resultMessage = Util.null2String(resultJson.getString("message"));
                if (!"success".equals(resultMessage)) {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                } else {
                    JSONObject resultJsonObj = resultJson.getJSONObject("data");
                    String errorCode = resultJsonObj.getString("errorCode");
                    if (!RES_CODE_ZERO.equals(errorCode)) {
                        message += Util.null2String(resultJsonObj.getString("errorMessage"));
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>Wea02CusSpecApproReaPhaseAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}
